/*
 * @Author: wangzhijian
 * @Date: 2025-04-09 15:24:26
 * @LastEditTime: 2025-04-09 15:35:02
 */
import mysql from 'mysql2/promise';
import dotenv from 'dotenv';

dotenv.config();

async function initializeDatabase() {
    const connection = await mysql.createConnection({
        host: process.env.DB_HOST,
        user: process.env.DB_USER,
        password: process.env.DB_PASSWORD,
        port: Number(process.env.DB_PORT)
    });

    try {
        // Create database if it doesn't exist
        await connection.query(`CREATE DATABASE IF NOT EXISTS ${process.env.DB_NAME}`);
        await connection.query(`USE ${process.env.DB_NAME}`);

        // Create calculations table
        await connection.query(`
            CREATE TABLE IF NOT EXISTS calculations (
                id INT AUTO_INCREMENT PRIMARY KEY,
                product_id VARCHAR(50) NOT NULL,
                profit_value DECIMAL(10, 2) NOT NULL,
                profit_type ENUM('fixed', 'cost', 'price') NOT NULL,
                cost DECIMAL(10, 2) NOT NULL,
                coupon DECIMAL(10, 2) NOT NULL,
                off DECIMAL(10, 2),
                y DECIMAL(10, 2),
                weight DECIMAL(10, 2) NOT NULL,
                withdrawal_fee_rate DECIMAL(10, 4) DEFAULT 0.01,
                platform_commission_rate DECIMAL(10, 4) DEFAULT 0.054,
                sfp_fee_rate DECIMAL(10, 4) DEFAULT 0,
                talent_commission_rate DECIMAL(10, 4) DEFAULT 0.01,
                transaction_fee_rate DECIMAL(10, 4) DEFAULT 0.0378,
                vat_rate DECIMAL(10, 4) DEFAULT 0.1,
                bonus_cashback_rate DECIMAL(10, 4) DEFAULT 0.0435,
                logistics_cost_per_10g DECIMAL(10, 2) DEFAULT 0.15,
                exchange_rate DECIMAL(10, 4) DEFAULT 1.6412,
                buyer_shipping_fee DECIMAL(10, 2) DEFAULT 10,
                result TEXT,
                type ENUM('pricing', 'adjustment') NOT NULL,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                INDEX idx_product_id (product_id)
            )
        `);

        console.log('Database initialized successfully');
    } catch (error) {
        console.error('Error initializing database:', error);
    } finally {
        await connection.end();
    }
}

initializeDatabase(); 